{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'tom', 'age': '12', 'gender': True, 'hobby': 'eat'}\n"
     ]
    }
   ],
   "source": [
    "# 字面量创建\n",
    "d1 = {}\n",
    "d2 = {'name':'tom','age':'12','gender':True,'hobby':'eat'}\n",
    "print(d2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'jack', 'age': '999'}\n",
      "{'name': 'rose', 'age': 90}\n",
      "{'name': 'wjh', 'age': '23', 'sex': 'male', 'address': 'beijing'}\n",
      "wjh\n"
     ]
    }
   ],
   "source": [
    "# 构造方法定义字典\n",
    "d3 = dict(name='jack',age='999')\n",
    "d4 = dict([('name','rose'),['age',90]])\n",
    "keys=('name','age','sex','address')\n",
    "values=['wjh','23','male','beijing']\n",
    "d5 = dict(zip(keys,values))\n",
    "print(d3)\n",
    "print(d4)\n",
    "print(d5)\n",
    "print(d5['name'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'hhh', 'age': '23', 'sex': 'male', 'address': 'beijing'}\n"
     ]
    }
   ],
   "source": [
    "# 字典增加和修改  第一次使用key时新加一个，key已存在时做修改\n",
    "d5['name']='hhh'\n",
    "print(d5)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "# 删除 del list_name['key']  删除对应字典的一个键值对\n",
    "# del list_name 删除整个字典\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 字典数据获取类操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['name', 'age', 'sex', 'address'])\n",
      "<class 'dict_keys'>\n",
      "dict_values(['tom', '23', 'male', 'beijing'])\n",
      "<class 'dict_values'>\n",
      "dict_items([('name', 'tom'), ('age', '23'), ('sex', 'male'), ('address', 'beijing')])\n",
      "<class 'dict_items'>\n",
      "None\n",
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "stu = {'name': 'tom', 'age': '23', 'sex': 'male', 'address': 'beijing'}\n",
    "#key()  保存到一个列表中  dict_keys类型\n",
    "print(stu.keys())\n",
    "print(type(stu.keys()))\n",
    "#values()   保存到一个列表中   dict_values类型\n",
    "print(stu.values())\n",
    "print(type(stu.values()))\n",
    "#items()   获取字典中所有键值对，每一个元素键值对都以一个元组保存，见所有元组保存到一个列表中  dict_items()保存\n",
    "print(stu.items())\n",
    "print(type(stu.items()))\n",
    "#get(key,default)   获取key对应的值key不存在则返回默认值\n",
    "print(stu.get('name'))\n",
    "print(type(stu.get('name')))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 字典添加更新"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "# setdefault(key,default_values)   key不存在则新增key并设置默认值，若key存在就啥也不干"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': None, 'age': None, 'sex': None, 'address': None}\n"
     ]
    }
   ],
   "source": [
    "# fromkeys(keys,val)  创建新字典  以序列keys的元素作为key，以val作为所有键的默认值，val默认值为None    静态方法  需要以字典类型名dict调用\n",
    "new_dict=dict.fromkeys(('name','age','sex','address'))\n",
    "print(new_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '空', 'age': '空', 'sex': '空', 'address': '空'}\n"
     ]
    }
   ],
   "source": [
    "new_dict=dict.fromkeys(['name','age','sex','address'],'空')\n",
    "print(new_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "# update()更新字典  存在则修改  不存在则新加键值对"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 字典删除类操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'tom', 'age': '23', 'sex': 'male', 'address': 'beijing'}\n",
      "('address', 'beijing')\n",
      "{'name': 'tom', 'age': '23', 'sex': 'male'}\n"
     ]
    }
   ],
   "source": [
    "# popitem()获取并删除最后一个键值对 放元组里了\n",
    "stu = {'name': 'tom', 'age': '23', 'sex': 'male', 'address': 'beijing'}\n",
    "print(stu)\n",
    "d=stu.popitem()\n",
    "print(d)\n",
    "print(stu)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'tom', 'age': '23', 'sex': 'male', 'address': 'beijing'}\n",
      "tom\n",
      "{'age': '23', 'sex': 'male', 'address': 'beijing'}\n"
     ]
    }
   ],
   "source": [
    "# pop(key) 删除指定键值对并获取其value\n",
    "stu = {'name': 'tom', 'age': '23', 'sex': 'male', 'address': 'beijing'}\n",
    "print(stu)\n",
    "d=stu.pop('name')\n",
    "print(d)\n",
    "print(stu)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# clear  q清空字典"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}